package com.jsmile.cloud.accountcenter.account.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jsmile.cloud.accountcenter.account.service.AccountApplicationService;
import com.jsmile.cloud.accountcenter.api.enums.AccountCodeEnum;
import com.jsmile.cloud.accountcenter.api.exception.AccountException;
import com.jsmile.cloud.accountcenter.api.req.ReqMemberRegister;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.log.annotation.JsmileLog;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
public class AccountServiceApiImpl extends AuthcController {

    private final AccountApplicationService accountApplicationService;

    @RequestMapping(value = "/feign/account/register", method = RequestMethod.POST)
    @JsmileLog(title = "账户中心->账户注册", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.ACCOUNT_CENTER)
    public JSmileResult register(@RequestBody @Valid ReqMemberRegister reqMemberRegister, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.ACCOUNT_CENTER);
        if (accountApplicationService.register(reqMemberRegister)) {
            return JSmileResult.success();
        }
        throw new AccountException(AccountCodeEnum.WX_MEMBER_SAVE_ERROR);
    }

}
